package com.cloudspace.jindun.adapter;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.cloudspace.jindun.JindunApp;
import com.cloudspace.jindun.R;
import com.cloudspace.jindun.activity.dianhua.LinkmanActivity;
import com.cloudspace.jindun.config.Configs;
import com.cloudspace.jindun.manager.ContactPersonManager;
import com.cloudspace.jindun.model.Friends;
import com.cloudspace.jindun.net.manager.ImageManager;
import com.cloudspace.jindun.user.UserSessionManager;
import com.cloudspace.jindun.utils.ObjUtil;
import com.cloudspace.jindun.utils.PinyinUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by zengxianhua on 16/12/25.
 */

public class TopicAdatper extends BaseAdapter {
    private List<Friends.DataBean> datas;
    private FragmentActivity activity;
    private List<Integer> checked;


    private View.OnClickListener detailListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Friends friends = (Friends) v.getTag();
            if (friends != null && activity != null) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("friends", friends);

                Intent intent = new Intent(activity, LinkmanActivity.class);
                intent.putExtras(bundle);
                activity.startActivity(intent);
            }
        }
    };

    public List<Integer> getChecked() {
        return checked;
    }

    public TopicAdatper(FragmentActivity activity) {
        this.activity = activity;
    }

    public void setDatas(List<Friends.DataBean> data) {
        if (ObjUtil.isEmpty(data)) {
            return;
        }

        this.datas = data;
        checked = new ArrayList<Integer>(
                Collections.nCopies(data.size(), 0));
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (ObjUtil.isEmpty(datas)) {
            return 0;
        }

        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        if (ObjUtil.isEmpty(datas)) {
            return null;
        }

        if (position < 0 || position >= datas.size()) {
            return null;
        }

        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(JindunApp.getInstance()).inflate(R.layout.item_group_member_add, null);
            holder.icon = (ImageView) convertView.findViewById(R.id.phone_image_header);
            holder.name = (TextView) convertView.findViewById(R.id.phone_textview_name);
            holder.firstLetter = (TextView) convertView.findViewById(R.id.firstWord);
            holder.cb_addMember = (CheckBox) convertView.findViewById(R.id.cb_addMember);
            holder.checkly = convertView.findViewById(R.id.checkly);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Friends.DataBean dataBean = datas.get(position);


        holder.name.setText(datas.get(position).getUserName());
        ImageManager.loadCircleImage(JindunApp.getInstance(), holder.icon, dataBean.getIconUrl(), Configs.IMAGE_CORNER_RADIUS);

        String currentFirstWord = "";
        String preFirstWord = "";
        if (dataBean != null && !TextUtils.isEmpty(dataBean.getUserName())) {
            currentFirstWord = PinyinUtils.getPinyinFirstLetter(dataBean.getUserName());
        }


        if (!TextUtils.isEmpty(currentFirstWord)) {
            if (position == 0) {
                holder.firstLetter.setVisibility(View.VISIBLE);
                holder.firstLetter.setText(currentFirstWord);
            } else {
                //后一个与前一个对比,判断首字母是否相同，相同则隐藏
                preFirstWord = PinyinUtils.getPinyinFirstLetter(datas.get(position - 1).getUserName());
                if (currentFirstWord.equals(preFirstWord)) {
                    holder.firstLetter.setVisibility(View.GONE);
                } else {
                    holder.firstLetter.setVisibility(View.VISIBLE);
                    holder.firstLetter.setText(currentFirstWord);
                }
            }
        }

        boolean inContact = !ObjUtil.isEmpty(ContactPersonManager.getInstance().queryByUserId(dataBean.getUserId()));
        boolean isMe = UserSessionManager.getInstance().getUserId().equals(dataBean.getUserId());

        holder.checkly.setTag(holder);
        holder.checkly.setTag(R.id.position, position);
        holder.checkly.setOnClickListener(cbViewClickListener);


        return convertView;
    }

    private View.OnClickListener cbViewClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v == null || v.getTag() == null || !(v.getTag() instanceof  ViewHolder)){
                return;
            }

            ViewHolder holder = (ViewHolder) v.getTag();
            if (holder != null){
                holder.cb_addMember.setChecked(!holder.cb_addMember.isChecked());
            }

            Integer position = (Integer) v.getTag(R.id.position);
            if (!ObjUtil.isEmpty(checked) &&  position >= 0 && position < checked.size()){
                checked.set(position, holder.cb_addMember.isChecked()?1:0);
            }
        }
    };

    static class ViewHolder {
        ImageView icon;
        TextView name;
        TextView firstLetter;
        CheckBox cb_addMember;
        View checkly;
    }
}